package stack;

//模拟实现一个栈
//可以基于顺序表来实现,也可以给基于链表来实现
//用数组来实现是最简单的
public class MyStack {
    private String [] arr;
    private int size;

    public MyStack(){
        arr = new String[1000];
        size = 0;
    }

    public MyStack(int capability){
        arr = new String[capability];
        size = 0;
    }

    //入栈
    public void push(String elem){
        if(size == arr.length){
            resize();
        }
        arr[size] = elem;
        size++;
    }

    private void resize(){
        String [] newArr = new String[2 * arr.length];
        for(int i = 0;i < arr.length;i++){
            newArr[i] = arr[i];
        }
        arr = newArr;
    }
    //出栈
    public String pop(){
        if(size == 0){
            throw new RuntimeException("Stack is empty!");
        }
        String elem = arr[size - 1];
        size--;
        return elem;
    }

    //取栈顶元素
    public String peek() {
        if (size == 0) {
            throw new RuntimeException("Stack is empty!");
        }
        String elem = arr[size - 1];
        return elem;
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack();
//        stack.push("aaa");
//        stack.push("bbb");
//        stack.push("ccc");
        System.out.println(stack.peek());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());

    }
}
